package com.jtpay.sth0409.viewdraghelper;

import android.content.Context;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Created by sth0409 on 2017/4/24.13:14.
 */

public class MyDragViewLayout extends LinearLayout {
    private ViewDragHelper viewDragHelper;

    private boolean isOpen;
    private ViewDragHelper.Callback callback;
    private View mDragView;


    private Point mAutoBackOriginPos = new Point();
    private View view1;
    private View view2;
    private View view3;


    public MyDragViewLayout(Context context) {
        super(context);
        init(context);
    }


    public MyDragViewLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyDragViewLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return viewDragHelper.shouldInterceptTouchEvent(ev);
    }

    //onTouchEvent方法中调用ViewDragHelper.processTouchEvent方法并返回true
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }


    private void init(Context context) {
        callback = new ViewDragHelperCallBack();
        viewDragHelper = ViewDragHelper.create(this, 1.0f, callback);
        viewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }


    class ViewDragHelperCallBack extends ViewDragHelper.Callback {

        @Override
        public int getViewHorizontalDragRange(View child) {
            return getMeasuredWidth() - child.getMeasuredWidth();
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return getMeasuredHeight()-child.getMeasuredHeight();
        }

        @Override
        public boolean tryCaptureView(View child, int pointerId) {

            return child == view1 || child == view2;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            final int leftBound = getPaddingLeft();
            final int rightBound = getWidth() - child.getWidth() - leftBound;

            final int newLeft = Math.min(Math.max(left, leftBound), rightBound);

            return newLeft;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            int topBound = getPaddingTop();
            int bottom = getHeight() - child.getHeight() - topBound;
            int newTop = Math.min(Math.max(top, topBound), bottom);


            return newTop;
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            if (releasedChild == view2) {
              int xx=releasedChild.getLeft();
              int yy=releasedChild.getTop();

                int xxx = (int) (xx - mAutoBackOriginPos.x);
                int yyy = (int) (yy - mAutoBackOriginPos.y);
                Log.i("--------", "onViewReleased: " + Math.abs(xxx) + "***" + Math.abs(yyy));
                if (Math.abs(xxx) < 800 && Math.abs(yyy) < 800) {
                    viewDragHelper.settleCapturedViewAt(mAutoBackOriginPos.x, mAutoBackOriginPos.y);
                }
                invalidate();
            }

        }

        //在边界拖动时回调
        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
            viewDragHelper.captureChildView(view3, pointerId);
        }



    }

    @Override
    public void computeScroll() {
        if (viewDragHelper.continueSettling(true)) {
            invalidate();
        }
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mAutoBackOriginPos.x = view2.getLeft();
        mAutoBackOriginPos.y = view2.getTop();


    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        view1 = getChildAt(0);
        view2 = getChildAt(1);
        view3 = getChildAt(2);


    }
}
